package com.app.newiptv.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.newiptv.R;
import com.app.newiptv.adaper.PlayDetailAdapter;
import com.app.newiptv.base.MyApplication;
import com.app.newiptv.presenter.PlayDetailsPresenter;
import com.app.newiptv.utils.APPLog;
import com.app.newiptv.utils.Paths;
import com.bumptech.glide.Glide;


public class FilmDetailsActivity extends Activity implements View.OnClickListener, View.OnFocusChangeListener {
    private RecyclerView mRecommend_rv;
    private PlayDetailsPresenter playDetailsPresenter;
    private Button mPlay_btn;
    private Button mCollection_btn;
    private ImageView focusView;
    private int id;
    private ImageView mImg_view;
    private TextView mFilm_name;
    private TextView mDirector_tv;
    private TextView to_star_tv;
    private TextView years_tv;
    private TextView introduce_tv;
    private TextView clear_type;
    private TextView copyright_text;
    private TextView text_groom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_play_details);
        Bundle bundle = getIntent().getBundleExtra("bundle");
        id = bundle.getInt("id", 1);
        initView();
        initData();
        setLinsenter();
    }

    private void initView() {
        mRecommend_rv = (RecyclerView) findViewById(R.id.recommend_rv);
        mPlay_btn = (Button) findViewById(R.id.play_btn);
        mCollection_btn = (Button) findViewById(R.id.collection_btn);
        focusView = (ImageView) findViewById(R.id.focus_view);

        mImg_view = (ImageView) findViewById(R.id.img_view);
        mFilm_name = (TextView) findViewById(R.id.film_name);
        mDirector_tv = (TextView) findViewById(R.id.director_tv);
        to_star_tv = (TextView) findViewById(R.id.to_star_tv);
        years_tv = (TextView) findViewById(R.id.years_tv);
        introduce_tv = (TextView) findViewById(R.id.introduce_tv);
        clear_type = (TextView) findViewById(R.id.clear_type);
        copyright_text = (TextView) findViewById(R.id.copyright_text);
        text_groom = (TextView) findViewById(R.id.text_groom);
    }

    private void initData() {
        clear_type.setTypeface(MyApplication.face);
        copyright_text.setTypeface(MyApplication.face);
        text_groom.setTypeface(MyApplication.face);
        playDetailsPresenter = new PlayDetailsPresenter(this);
        playDetailsPresenter.setDisplayed(true);
        String url = Paths.HOSTPATH + Paths.VIDEO_PATH + "/" + id;
        String hotUrl = Paths.HOSTPATH + Paths.HOT_PAHT + "/" + 20;
        APPLog.printInfo("hotUrl==>:" + hotUrl);
        playDetailsPresenter.loadDetailContent(url, detailCallback);
        playDetailsPresenter.loadListItem(hotUrl, detailCallback);
        mCollection_btn.setNextFocusDownId(R.id.recommend_rv);
        mCollection_btn.setNextFocusRightId(R.id.collection_btn);
        mPlay_btn.setNextFocusDownId(R.id.recommend_rv);
        mPlay_btn.setNextFocusLeftId(R.id.play_btn);
    }

    private void setLinsenter() {
        mPlay_btn.setOnClickListener(this);
        mCollection_btn.setOnClickListener(this);
        mPlay_btn.setOnFocusChangeListener(this);
        mCollection_btn.setOnFocusChangeListener(this);
    }

    private PlayDetailsPresenter.Callback detailCallback = new PlayDetailsPresenter.Callback() {
        @Override
        public void onError(int code, String msg) {

        }

        @Override
        public void onMovieCompleted(PlayDetailsPresenter presenter, int count) {
            PlayDetailAdapter playDetailAdapter = new PlayDetailAdapter(presenter, focusView, FilmDetailsActivity.this);
            playDetailAdapter.setCount(count);
            LinearLayoutManager manager = new LinearLayoutManager(FilmDetailsActivity.this);
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            mRecommend_rv.setLayoutManager(manager);
            mRecommend_rv.setAdapter(playDetailAdapter);
            mRecommend_rv.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        if (mRecommend_rv.getChildCount() > 0) {
                            mRecommend_rv.getChildAt(0).requestFocus();
                        }
                    }
                }
            });
        }

        @Override
        public void onDetailCompleted(PlayDetailsPresenter presenter) {
            presenter.onBindDetailData(new OnBindDetail());
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.play_btn:
                playDetailsPresenter.toPlay();
                break;
            case R.id.collection_btn:
                playDetailsPresenter.toCollection();
                break;
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        switch (v.getId()) {
            case R.id.play_btn:
                if (hasFocus) {
                    mPlay_btn.setBackgroundResource(R.mipmap.pd_play_focused);
                } else {
                    mPlay_btn.setBackgroundResource(R.mipmap.pd_play_normal);
                }
                break;
            case R.id.collection_btn:
                if (hasFocus) {
                    mCollection_btn.setBackgroundResource(R.mipmap.pd_collect_focused);
                } else {
                    mCollection_btn.setBackgroundResource(R.mipmap.pd_collect_normal);
                }
                break;
        }
    }

    private class OnBindDetail implements PlayDetailsPresenter.IPlayDetailsViewHolder {

        @Override
        public void setImg_view(String url) {
            Log.d("tag", "-----------url---------" + url);
            Glide.with(FilmDetailsActivity.this).load(Paths.HOSTPATH + url).into(mImg_view);
        }

        @Override
        public void setFilm_name(String text) {
            mFilm_name.setTypeface(MyApplication.face);
            mFilm_name.setText(text);
        }

        @Override
        public void setDirector_tv(String text) {
            mDirector_tv.setTypeface(MyApplication.face);
            mDirector_tv.setText(text);
        }

        @Override
        public void setTostar_tv(String text) {
            to_star_tv.setTypeface(MyApplication.face);
            to_star_tv.setText(text);
        }

        @Override
        public void setYears_tv(String text) {
            years_tv.setTypeface(MyApplication.face);
            years_tv.setText(text);
        }

        @Override
        public void setIntroduce_tv(String text) {
            introduce_tv.setTypeface(MyApplication.face);
            introduce_tv.setText(text);
        }

        @Override
        public void setCopyright(String copyright) {
            copyright_text.setTypeface(MyApplication.face);
            copyright_text.setText("版权来源：" + copyright);
        }
    }
}
